﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProductionManagement.Application.Command.DispatchCommand;
using ProductionManagement.Domain.Dtos.Dispathch;
using ProductionManagement.Dto;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProductionManagement.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class DisPatchController : ControllerBase
    {
        IMediator _mediator;


        public DisPatchController(IMediator mediator)
        {
            _mediator = mediator;
        }


        /// <summary>
        /// 新增派工
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ERPPublic.ApiResult<int>> AddDisPatch(AddDispathchDTO command)
        {
            ERPPublic.ApiResult<int> result = new ERPPublic.ApiResult<int>();

            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //获取用户ID
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.Office_Id = sid;
                command.User_Id = int.Parse(userId);
                command.AddName = name;
                result.Data = await _mediator.Send(new CreateDiapatchCommand { AddDto = command });
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示派工
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ERPPublic.ApiResult<PageDto<ShowDispathchDTO>>> ShowDisPatch([FromQuery]ShowDispatchCommand command)
        {
            ERPPublic.ApiResult<PageDto<ShowDispathchDTO>> result = new ERPPublic.ApiResult<PageDto<ShowDispathchDTO>>();

            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 反填派工
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ERPPublic.ApiResult<List<ShowDispathchDTO>>> BlikDisPatch([FromQuery]BlikDispatchCommand command)
        {
            ERPPublic.ApiResult<List<ShowDispathchDTO>> result = new ERPPublic.ApiResult<List<ShowDispathchDTO>>();

            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 修改派工
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ERPPublic.ApiResult<int>> UpdDisPatch(AddDispathchDTO command)
        {
            ERPPublic.ApiResult<int> result = new ERPPublic.ApiResult<int>();

            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //获取用户ID
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.Office_Id = sid;
                command.User_Id = int.Parse(userId);
                command.UpdName = name;
                command.UpdTime=DateTime.Now;
                result.Data = await _mediator.Send(new UpdateDiapatchCommand { AddDto = command });
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除派工
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ERPPublic.ApiResult<int>> DelDisPatch(DelDiapatchCommand command)
        {
            ERPPublic.ApiResult<int> result = new ERPPublic.ApiResult<int>();

            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //获取用户ID
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.DelName = name;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
